(function (window) {
    function Lyric(mid) {
        return new Lyric.prototype.init(mid);
    }
    Lyric.prototype = {
        constructor: Lyric,
        times: [],
        lyrics: [],
        index: -1,
        init: function (mid) {
            this.mid = mid;
        },
        loadLyric: function (callBack) {
            var $this = this;
            p(`http://47.97.121.9:3300/getLyric?songmid=${this.mid}`)
                .then(res => {
                    $this.parseLyric(res.response.lyric);
                    callBack();
                })
        },
        parseLyric: function (data) {
            var $this = this;
            // 清空上一首歌曲的歌词和时间
            $this.times = [];
            $this.lyrics = [];
            if (!data) {
                data = '[00: 00: 00]此歌曲为没有填词的纯音乐，请您欣赏';
            }
            var array = data.split('\n');
            var timeReg = /\d*:\d*\.\d*/;
            // 遍历取出每一条歌词
            $(array).each(function (index, value) {
                // 处理歌词
                var lyc = value.split(']')[1];
                // 排除空字符串（没有歌词的）
                if (lyc == '') return;
                $this.lyrics.push(lyc);

                var res = timeReg.exec(value);
                if (res == null) return true;
                var timeStr = res[0].split(':');
                var min = parseInt(timeStr[0] * 60);
                var sec = parseFloat(timeStr[1]);
                var time = parseFloat(Number(min + sec).toFixed(2));
                $this.times.push(time);
            })
        },
        currentIndex: function (currentTime) {
            if (currentTime >= this.times[0]) {
                this.index++;
                // 删除数组最前面一个元素
                this.times.shift();
            }
            return this.index;
        }
    }
    Lyric.prototype.init.prototype = Lyric.prototype;
    window.Lyric = Lyric;
})(window)